package com.boshao.classschedule.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.boshao.classschedule.R;
import com.boshao.classschedule.utils.ViewHolder;

/**
 * 课程选择单周、双周的dialog
 * @author boshao
 *
 */
public class SelectWeekDialogFragment extends DialogFragment implements OnClickListener,OnItemClickListener{
	public static final String TAG = "select_weekdialog_fragment";
	private View mRootView;
	private GridView mGridView;
	private TextView mTvCancel,mTvSave;
	public static final String[] WEEKDAYS = {"周日","周一","周二","周三","周四","周五","周六"};
	private ClassScheduleFragment mFragment;
	private DialogAdapter mDialogAdapter;
	public static SelectWeekDialogFragment newInstance(ClassScheduleFragment frament){
		SelectWeekDialogFragment fragment = new SelectWeekDialogFragment();
		fragment.setClassScheduldeFragment(frament);
		return fragment;
	}
	
	public void setClassScheduldeFragment(ClassScheduleFragment frament){
		mFragment = frament;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(STYLE_NO_TITLE,0);
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mRootView = inflater.inflate(R.layout.dialogfragment_repeat, container,false);
		mGridView = (GridView) mRootView.findViewById(R.id.gv_week);
		mRootView.findViewById(R.id.ll_choice).setVisibility(View.GONE);
		mDialogAdapter = new DialogAdapter();
		mGridView.setAdapter(mDialogAdapter);
		mGridView.setOnItemClickListener(this);
		mTvCancel = (TextView) mRootView.findViewById(R.id.tv_cancel);
		mTvCancel.setOnClickListener(this);
		mTvSave = (TextView) mRootView.findViewById(R.id.tv_save);
		mTvSave.setOnClickListener(this);
		return mRootView;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_save:
			save();
			break;
		case R.id.tv_cancel:
			mDialogAdapter.setSelectedPos(-1);
			dismiss();
			break;
		}
	}

	private void save() {
		int pos = ((DialogAdapter)mGridView.getAdapter()).getSelectedPos();
		if( pos != -1){
			mFragment.getWeekTextView().setText(String.format("第%d周",pos+1));	
			mFragment.changeWeeksHeaderView(pos+1);
		}
		dismiss();
	}

	class DialogAdapter extends BaseAdapter{
		private int mSelectedPos = -1;
		public DialogAdapter() {
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = LayoutInflater.from(getActivity()).inflate(R.layout.gridview_weeks,parent, false);
			}
			TextView week = ViewHolder.get(convertView,R.id.tv_week);
			ImageView iv = ViewHolder.get(convertView,R.id.iv_week);
			if(mSelectedPos == position){
				iv.setBackgroundDrawable(getResources().getDrawable(R.drawable.blue_circle_shap));
			}else{
				iv.setBackgroundDrawable(getResources().getDrawable(R.drawable.gray_circle_shap));
			}
			week.setText(String.valueOf(position+1));
			return convertView;
		}			
		
		public void setSelectedPos(int selectedPos){
			mSelectedPos = selectedPos;
		}		
		
		public int getSelectedPos(){
			return mSelectedPos;
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}			
		@Override
		public Object getItem(int position) {
			return null;
		}			
		@Override
		public int getCount() {
			return 21;
		}
		
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		DialogAdapter dialogAdapter = ((DialogAdapter)parent.getAdapter());
		dialogAdapter.setSelectedPos(position);
		dialogAdapter.notifyDataSetChanged();
	}
}
